//
//  RSSingleLineCell.m
//  StaffTax
//
//  Created by 侯伟 on 2020/2/20.
//  Copyright © 2020 侯伟. All rights reserved.
//

#import "RSSingleLineCell.h"

@interface RSSingleLineCell ()

/// 标题
@property (nonatomic,weak)UILabel* titleLb;

/// 内容文本
@property (nonatomic,weak)UILabel* subtitleLb;

/// 箭头
@property (nonatomic,weak)UIImageView* moreImage;

/// 分割线
@property (nonatomic,weak)UIView* separatorLine;

@end

@implementation RSSingleLineCell


+ (instancetype)cellWithTitle:(NSString *)title{
    RSSingleLineCell* cell = [RSSingleLineCell new];
    cell.title = title;
    return cell;
}

- (instancetype)init{
    if(self = [super init]){
        
        [self initViews];
        
        [self initDatas];
        
        self.backgroundColor = UIColor.whiteColor;
        
        self.myHeight = PX(50);
        
        self.padding = UIEdgeInsetsMake(0, 16, 0, 16);
        
    }
    return self;
}

- (void)initDatas{
    
    self.titleFont = FONT_FIT(14);
     
    self.titleColor = COLOR_BLACK_DARK;
    
    self.subtitleFont = FONT_FIT(14);
     
    self.subtitleColor = COLOR_GRAY_BASE;
    
    self.showSeparator = YES;
    
    self.clickable = YES;
}

- (void)initViews{
            
    UILabel* titleLb = [UILabel new];
    titleLb.textAlignment = NSTextAlignmentLeft;
    titleLb.myLeft = 0;
    titleLb.myCenterY = 0;
    [self addSubview:titleLb];
    _titleLb = titleLb;
    
    UIImageView* moreImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"common_more_gray"]];
    moreImageView.myRight = 0;
    moreImageView.myCenterY = 0;
    [self addSubview:moreImageView];
    _moreImage = moreImageView;
    
    UILabel* subtitleLb = [UILabel new];
    subtitleLb.textAlignment = NSTextAlignmentRight;
    subtitleLb.myCenterY = 0;
    subtitleLb.leftPos.equalTo(titleLb.rightPos).offset(15);
    subtitleLb.rightPos.equalTo(moreImageView.leftPos).offset(8.0);
    [self addSubview:subtitleLb];
    _subtitleLb = subtitleLb;
    
    UIView* separatorLine = [UIView new];
    separatorLine.myHorzMargin = 0;
    separatorLine.myBottom = 0;
    separatorLine.backgroundColor = COLOR_GRAY_LINE;
    separatorLine.myHeight = Size_1PX;
    [self addSubview:separatorLine];
    _separatorLine = separatorLine;
}

#pragma mark --setter

- (void)setTitle:(NSString *)title{
    _title = title;
    self.titleLb.text = title;
    [self.titleLb sizeToFit];
}

- (void)setTitleFont:(UIFont *)titleFont{
    _titleFont = titleFont;
    self.titleLb.font = titleFont;
    [self.titleLb sizeToFit];
}

- (void)setTitleColor:(UIColor *)titleColor{
    _titleColor = titleColor;
    self.titleLb.textColor = titleColor;
}

- (void)setSubtitle:(NSString *)subtitle{

    self.subtitleLb.text = subtitle;
    [self.subtitleLb sizeToFit];
}

- (void)setAttrSubtitle:(NSAttributedString *)attrSubtitle{
    self.subtitleLb.attributedText = attrSubtitle;
    [self.subtitleLb sizeToFit];
}

- (void)setSubtitleFont:(UIFont *)subtitleFont{
    _subtitleFont = subtitleFont;
    self.subtitleLb.font = subtitleFont;
    [self.subtitleLb sizeToFit];
    
}

- (void)setSubtitleColor:(UIColor *)subtitleColor{
    _subtitleColor = subtitleColor;
    self.subtitleLb.textColor = subtitleColor;
}

- (void)setShowSeparator:(BOOL)showSeparator{
    _showSeparator = showSeparator;
    self.separatorLine.visibility = showSeparator?MyVisibility_Visible:MyVisibility_Gone;
}

- (void)setSeparatorInset:(UIEdgeInsets)separatorInset{
    _separatorInset = separatorInset;
    if(self.showSeparator){
        self.separatorLine.myLeft = separatorInset.left;
        self.separatorLine.myRight = separatorInset.right;
    }
}

- (void)setClickable:(BOOL)clickable{
    _clickable = clickable;
    self.moreImage.visibility = clickable?MyVisibility_Visible:MyVisibility_Gone;
    [self.moreImage sizeToFit];

    if(clickable){
        [self setTarget:self action:@selector(itemClicked:)];
    }else{
        [self setTarget:NULL action:@selector(itemClicked:)];
    }
}
#pragma mark --

- (void)itemClicked:(UIButton*)sender{

    if(!self.clickable)
        return;
    if(self.clickBlock){
        self.clickBlock();
    }
}

@end
